<?php
/**
 * Created by PhpStorm.
 * User: apple
 * Date: 2017/8/24
 * Time: 下午6:06
 */

namespace uploader;

class Uploader
{
    protected $typeAllowed = [

        'bx_fy2', // 二级发运数据
        'bx_wd', // 网点零售
    ];

    protected $type = '';

    protected $uploadRootPath = '';

    protected $uploadPath = '';

    protected $error = '';

    public function exec($files)
    {
        if (empty($files)) {
            $this->error = '文件提交失败';
            return false;
        }

        $data = [];
        foreach ($files as $type => $file) {
            if (in_array($type, $this->typeAllowed)) {
                $this->type = $type;
                $method = $type . 'Handle';
                method_exists($this, $method) || $method = 'defaultHandle';

                $data[] = call_user_func([$this, $method], $file);
            }
        }
        if (count($data) == 1) {
            $data = $data[0];
            if (isset($data['error'])) {
                $this->error = $data['error'];
                unset($data['error']);
            }
        } else {
            $this->error = '文件名不合法';
        }
        return $data;
    }

    /**
     * @param $file \think\File
     */
    protected function defaultHandle($file)
    {
        $this->uploadRootPath = ROOT_PATH . 'public' . DS . 'uploads' . DS;
        empty($this->uploadPath) && $this->uploadPath = $this->uploadRootPath . $this->type;
        $result = [
            'filename' => $file->getInfo('name'),
            'size' => $file->getSize(),
        ];
        $info = $file->move( $this->uploadPath );
        if (! $info) {
            $result['error'] = $file->getError();
        } else {
            $result = $result + [
                    'uri' => DS . 'uploads' . DS . $this->type . DS . $info->getSaveName(),
                    'extension' => $info->getExtension(),
                ];
        }
        return $result;
    }

    /**
     * @param $file \think\File
     * @return array
     */
    protected function userProfileHandle($file)
    {
        $file->validate([
            'size' => 500000,
            'ext' => 'jpg,png,gif,jpeg,xls'
        ]);
        return $this->defaultHandle($file);
    }
//    protected function MaintenanceHitchHandle($file){
//
//        $file->validate([
//            'size' => 500000,
//            'ext' => 'jpg,png,gif,jpeg'
//        ]);
//        empty($this->uploadPath) && $this->uploadPath = $this->uploadRootPath . $this->type;
//        $result = [
//            'filename' => $file->getInfo('name'),
//            'size' => $file->getSize(),
//        ];
//        $info = $file->move( $this->uploadPath );
//        if (! $info) {
//
//            $result['error'] = $file->getError();
//        } else {
//            $imgSrc=$info->getSaveName();
//            $simg =  '.'.DS . 'uploads' . DS . $this->type . DS . $info->getSaveName();
//
//            $image = \think\Image::open($simg);
//
//            $water=date('Y-m-d H:i:s',time());//水印
//           $image->text('“' . $water, 'lib/uploadifive-1.22/vrinda.ttf', 14, '#000000', 7, - 60, 0)->save($simg);
//
//            $result = $result + [
//                    'uri' => DS . 'uploads' . DS . $this->type . DS . $info->getSaveName(),
//                    'extension' => $info->getExtension(),
//                ];
//        }
//        return $result;
//    }
    protected function maintainHandle($file)
    {
        $file->validate([
            'size' => 955678,
            'ext' => 'jpg,png,gif,xls,docx,xlsx'
        ]);
        return $this->defaultHandle($file);
    }


    public function getError()
    {
        return $this->error;
    }
}